﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Web.fanliAdmin.Info
{
    public partial class fanli_promotion : System.Web.UI.Page
    {
        #region DAL
        fanli.DAL.fanli_promotion dal = new fanli.DAL.fanli_promotion();
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            SessionHelp.AssertLogin(this);
            if (!IsPostBack)
            {
                BindCata();
                BindAdmin();
                GridBind();
            }
        }
        private void BindCata()
        {
            ddlShop.DataSource = (new fanli.DAL.fanli_shop()).GetDatasetBySql("select id,title from fanli_mall order by id", null);
            ddlShop.DataBind();
            ddlShop.Items.Insert(0, new ListItem("全部", "0"));
        }
        private void BindAdmin()
        {
            ddlAdmin.DataSource = (new fanli.DAL.fanli_adminUsers()).GetDatasetBySql("select id,realName from fanli_adminUsers ", null);
            ddlAdmin.DataBind();
            ddlAdmin.Items.Insert(0, new ListItem("全部", "0"));
        }
        private void GridBind()
        {
            StringBuilder sql = new StringBuilder();
            List<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter param = null;
            sql.Append("select p.*,mall.title as shopTitle,admin.realName from fanli_promotion as p,fanli_mall as mall,fanli_adminUsers as admin where p.shopID=mall.id and p.adminID=admin.ID ");

            if (ddlShop.SelectedValue != "0")
            {
                sql.Append(" and mall.id = @shopid");
                param = new SqlParameter("@shopid", ddlShop.SelectedValue);
                paramList.Add(param);
            }
            if (txtQryId.Text != "")
            {
                sql.Append(" and p.id = @id");
                param = new SqlParameter("@id", txtQryId.Text);
                paramList.Add(param);
            }
            if (txtQryTitle.Text != "")
            {
                sql.Append(" and news.title like '%' + @title + '%'");
                param = new SqlParameter("@cataname", txtQryTitle.Text);
                paramList.Add(param);
            }
            if (txtStart.Text != "" && txtEnd.Text != "")
            {
                sql.Append(" and datediff(day,convert(datetime,@startTime) ,news.pubDate)>=0 and datediff(day,convert(datetime,@endTime) ,news.pubDate)<=0");
                param = new SqlParameter("@startTime", txtStart.Text);
                paramList.Add(param);
                param = new SqlParameter("@endTime", txtEnd.Text);
                paramList.Add(param);
            }
            if (txtStartE.Text != "" && txtEndE.Text != "")
            {
                sql.Append(" and datediff(day,convert(datetime,@startTimeE) ,p.pubDate)>=0 and datediff(day,convert(datetime,@endTimeE) ,p.pubDate)<=0");
                param = new SqlParameter("@startTimeE", txtStartE.Text);
                paramList.Add(param);
                param = new SqlParameter("@endTimeE", txtEndE.Text);
                paramList.Add(param);
            }
            if (ddlAdmin.SelectedValue!="0")
            {
                sql.Append(" and admin.id=@adminID");
                param = new SqlParameter("@adminID", ddlAdmin.SelectedValue);
                paramList.Add(param);
            }

            sql.Append(" order by  p.pubDate desc ");

            DataSet ds = dal.GetDatasetBySql(sql.ToString(), paramList);
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;
            pds.AllowPaging = true;
            AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            pds.PageSize = AspNetPager1.PageSize;
            this.GridView1.DataSource = pds;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#E9F5FC'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            }
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            GridBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            if (dal.Delete(id) > 0)
            {
                GridBind();
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            GridBind();
        }
    }
}